//
//  UINavigationItem+Add.m
//  Merchant
//
//  Created by ibendi on 16/3/31.
//  Copyright © 2016年 ibendi. All rights reserved.
//

#import "UINavigationItem+Add.h"
#import <objc/runtime.h>

@implementation UINavigationItem (Add)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethodImp = class_getInstanceMethod(self, @selector(backBarButtonItem));
        Method destMethodImp = class_getInstanceMethod(self, @selector(myCustomBackButton_backBarbuttonItem));
        method_exchangeImplementations(originalMethodImp, destMethodImp);
    });
}

static char kCustomBackButtonKey;
- (UIBarButtonItem *)myCustomBackButton_backBarbuttonItem{
    UIBarButtonItem *item = [self myCustomBackButton_backBarbuttonItem];
    if (item) {
        return item;
    }
    item = objc_getAssociatedObject(self, &kCustomBackButtonKey);
    if (!item) {
        item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:NULL];
//        item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pin_shadow"] style:UIBarButtonItemStyleDone target:nil action:NULL];
        objc_setAssociatedObject(self, &kCustomBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return item;
}



- (void)dealloc {
    objc_removeAssociatedObjects(self);
}

@end
